package main

// 优雅的通知子gorutine退出 利用channel
import (
	"fmt"
	"sync"
	"time"
)

func f() {
	defer wg.Done()
	var n int = 0
FORLOOKUP:
	for {
		n++
		fmt.Println("## ", n)
		select {
		case <-exitChan:
			break FORLOOKUP
		default:
		}
		time.Sleep(1 * time.Second)
	}
}

var wg sync.WaitGroup

// var exitChan struct{}
var exitChan = make(chan bool, 1)

func main() {
	wg.Add(1)
	go f()
	time.Sleep(3 * time.Second)
	exitChan <- true
	wg.Wait() //阻塞
}
